/*-----------------------------------------------------------------------*/
/* Low level disk I/O module SKELETON for FatFs     (C)ChaN, 2019        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control modules to the FatFs module with a defined API.       */
/*-----------------------------------------------------------------------*/

#include "ff.h"			/* Obtains integer types */
#include "diskio.h"		/* Declarations of disk functions */
#include "sdcard_apm32.hpp"
#include "stdio.h"

/* Definitions of physical drive number for each drive */
#define SDCARD 0
#define SPIFLASH 1

/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{

	switch (pdrv) {
	case SDCARD :

		// translate the reslut code here

		return RES_OK;

	
	}
	return STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
	BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{
	DSTATUS stat = RES_ERROR;

	switch (pdrv) {
	case SDCARD :
		SDCard_APM32 *sdcard;
		sdcard = SDCard_APM32::getInstance();
		if ( sdcard != nullptr ){
			return RES_OK;
		}else{
			return STA_NOINIT;
		}

		
		return stat;

	
	}
	return STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	LBA_t sector,	/* Start sector in LBA */
	UINT count		/* Number of sectors to read */
)
{

	switch (pdrv) {
	case SDCARD :
		// translate the arguments here
		SDCard_APM32 *sdcard;
		sdcard = SDCard_APM32::getInstance();
		if ( sdcard == nullptr ) return RES_NOTRDY;
		for( ; count > 0; count-- ){
			if ( sdcard->readBlock(buff,sector,1) != 1 ){
				return RES_PARERR;
			}
			sector += 1;
		}
		return RES_OK;
	}
	return RES_PARERR;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber to identify the drive */
	const BYTE *buff,	/* Data to be written */
	LBA_t sector,		/* Start sector in LBA */
	UINT count			/* Number of sectors to write */
)
{

	switch (pdrv) {
	case SDCARD :
		SDCard_APM32 *sdcard;
		sdcard = SDCard_APM32::getInstance();
		if ( sdcard == nullptr ) return RES_NOTRDY;
		for ( ; count > 0; count-- ){
			uint32_t res = sdcard->writeBlock((unsigned char*)buff,sector,1);
			if ( res != 1 ){
				return RES_ERROR;
			}
			sector += 1;
		}
		return RES_OK;
		
	}

	return RES_PARERR;
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	DWORD *pdword = nullptr;
	WORD *pword = nullptr;
	if ( pdrv != SDCARD ) return RES_ERROR;
	SDCard_APM32 *sdcard;
	SDCard::CardInfo info;
	sdcard = SDCard_APM32::getInstance();
	if ( sdcard == nullptr ) return RES_NOTRDY;
	info = sdcard->getInfo();
	switch ( cmd ){
		case CTRL_SYNC:
			return RES_OK;
		case GET_SECTOR_COUNT:
			pdword = (DWORD*)buff;
			*pdword = info.capacity;
			return RES_OK;
		case GET_SECTOR_SIZE:
			pword = (WORD*)buff;
			*pword = info.blockSize;
			return RES_OK;
		case GET_BLOCK_SIZE:
			pword = (WORD*)buff;
			*pword = 1;
			return RES_OK;
		case CTRL_TRIM:
			return RES_PARERR;
	}
	return RES_PARERR;
}

extern "C"
DWORD get_fattime(void){
	return 0;
}